﻿#region

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

#endregion

namespace 五分钟计时
{
    public partial class Form2 : Form
    {
        private KeyboardHookLib _keyboardHook;

        private int Time2Ticks = 1;

        public Form2(Settings data)
        {
            InitializeComponent();
            Setting = data;
            BackColor = TransparencyKey = Color.FromArgb(254, 255, 255);
        }

        private Settings Setting { get; }

        private void Form2_Load(object sender, EventArgs e)
        {
            Location = Setting.位置;
            _keyboardHook = new KeyboardHookLib();
            _keyboardHook.InstallHook(KeyPress);
            label1.ForeColor = Setting.颜色;
            FontFamily zt = new FontFamily("arial");
            label1.Font = new Font(zt, Setting.字体大小);
            label1.BackColor = BackColor;
            TimeSpan ts = new TimeSpan(0, 0, Setting.倒计时);
            label1.Text = ts.ToString("g");
            Size = label1.Size;
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            _keyboardHook?.UninstallHook();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Pinvoke.GetForegroundWindow() == Handle)
                return;
            TopMost = true;
        }

        public new void KeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle)
        {
            handle = false; //预设不拦截任何键
            var key = (Keys) hookStruct.vkCode;

            if ((key == Keys.Down) || (key == Keys.Next))
                timer2.Enabled = true;
            if (key == Keys.Home)
            {
                timer2.Enabled = false;
                Time2Ticks = 1;
                TimeSpan ts = new TimeSpan(0, 0, Setting.倒计时);
                label1.Text = ts.ToString("g");
            }
            if (key == Keys.Escape)
            {
                TopMost = false;
                Close();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = new TimeSpan(0, 0, Setting.倒计时 - Time2Ticks);
            if (ts.TotalSeconds < 0)
            {
                timer2.Enabled = false;
                return;
            }
            label1.Text = ts.ToString("g");
            Time2Ticks++;
        }

        #region 窗口移动

        private Point mouseOff; //鼠标移动位置变量
        private bool leftFlag; //标签是否为左键

        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true; //点击左键按下时标注为true;
            }
        }

        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void Form2_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
                leftFlag = false; //释放鼠标后标注为false;
        }

        #endregion
    }

    public class MyLabel : Label
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            float vlblControlWidth;
            float vlblControlHeight;

            Color controlBackColor = BackColor;
            Pen labelBorderPen;
            SolidBrush labelBackColorBrush;

            labelBorderPen = new Pen(Color.Empty, 0);
            labelBackColorBrush = new SolidBrush(controlBackColor);


            SolidBrush labelForeColorBrush = new SolidBrush(ForeColor);
//            base.OnPaint(e);
            vlblControlWidth = Size.Width;
            vlblControlHeight = Size.Height;
            e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
            e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
            e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.DrawString(Text, Font, labelForeColorBrush, 0, 0);
        }
    }
}